package com.huawei.security.controller;

import com.huawei.security.model.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class UserController {

    private static final Log log = LogFactory.getLog(UserController.class);
    
    /**
     * 判断用户是否登录
     * @param currUser
     * @return
     */
    @RequestMapping(value = "/login.do",method= RequestMethod.POST)
    public String isLogin(User currUser){
        log.info("=======> " + currUser.toString());
        Subject user = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(currUser.getName(), currUser.getName());
        token.setRememberMe(true);
        try
        {
            user.login(token);
            return "redirect:/main";
        }catch (AuthenticationException e) {
            log.error("登录失败错误信息:"+e);
            token.clear();
            return "redirect:/login";
        }
    }
}